﻿module RangeOLD
open System

type Range (cells:obj[]) =
    member this.Cells = cells

    //member + ...

    member this.CountNotNull =
        let mutable n = 0
        for c in cells do
            if c<>null then n<-n+1
        n
    
    member this.CountFloat =
        let mutable tot = 0    
        for el in cells do
            match el with
            | :? float -> tot<-tot + 1
            | _ -> ()
        tot
       
    member this.MaxFloat =
        let mutable m = -infinity
        for el in cells do
            let temp = match el with
                       | :? float as num -> num
                       | _ -> nan
            if temp>m then m<-temp
        if m=(-infinity) then nan
        else m
    
    member this.MaxA =
        let mutable m = -infinity
        for el in cells do
            let temp = match el with
                        | :? float as num -> num
                        | :? bool as b -> if b then 1. else 0.
                        | :? string as s -> 
                            let mutable sf = 0.
                            if System.Double.TryParse(s, &sf) then sf
                            else nan
                        | _ -> nan
            if temp>m then m<-temp
        if m=(-infinity) then nan
        else m
        
    member this.MinFloat =
        let mutable m = infinity
        for el in cells do
            let temp = match el with
                        | :? float as num -> num
                        | _ -> nan
            if temp<m then m<-temp
        if m=infinity then nan
        else m
    
    member this.MinA =
        let mutable m = infinity
        for el in cells do
            let temp = match el with
                        | :? float as num -> num
                        | :? bool as b -> if b then 1. else 0.
                        | :? string as s -> 
                            let mutable sf = 0.
                            if System.Double.TryParse(s, &sf) then sf
                            else nan
                        | _ -> nan
            if temp<m then m<-temp
        if m=infinity then nan
        else m

    member this.SumFloat =
        let mutable tot = 0.    
        for el in cells do
            match el with
            | :? float as n -> tot <- tot + n
            | _ -> ()
        tot
    
    /// Sum inverse of numbers in the Range (ignore others)
    ///    return 1/a1 + 1/a2 .. + 1/an
    member this.SumFloatInverse = 
        let mutable tot = 0.    
        for el in cells do
            match el with
            | :? float as n -> tot<- tot + 1./n
            | _ -> ()
        tot

    member this.ProductFloat =
        let mutable tot = 1.
        for el in cells do
            match el with
            | :? float as n -> tot<-tot*n
            | _ -> ()
        tot
    